package com.ykt.controller;

import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.ykt.model.Camera;
import com.ykt.model.MediaSource;
import com.ykt.model.Video;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

@ControllerBind(controllerKey="/mediaSource")
public class MediaSourceController extends BaseController<MediaSource> {

	public MediaSourceController() {
		super(MediaSource.class);
	}
	
	public void findMediaSource() {
		
		Integer marketId = getParaToInt("marketId", -1);
		Integer shopId = getParaToInt("shopId", -1);
		Integer goodsId = getParaToInt("goodsId", -1);
		
		String bizType = getPara("bizType");
		
		Map<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("ms.biz_type", bizType);
		
		if(bizType.equals("MARKET")){
			paramMap.put("ms.biz_id", marketId.toString());
		}else if(bizType.equals("SHOP")){
			paramMap.put("ms.biz_id", shopId.toString());
		}else{
			paramMap.put("ms.biz_id", goodsId.toString());
		}
		
		int page = getParaToInt("page", 1);
        int rows = this.getParaToInt("rows", 20);
        try {
            Page<MediaSource> date = MediaSource.dao.pageFindModel(page, rows, paramMap);
            rendDataGrid(date);
        } catch (Exception e) {
            log.error(e);
            rendJson(false, e.getMessage());
        }
	}
	
	//保存视频
	public void savaMediaSource(){

		Integer marketId = getParaToInt("marketId", -1);
		Integer shopId = getParaToInt("shopId", -1);
		Integer goodsId = getParaToInt("goodsId", -1);

		String bizType = getPara("bizType");
		int bizId = 0;
		if(bizType.equals("MARKET")){
			bizId = marketId;
		}else if(bizType.equals("SHOP")){
			bizId = shopId;
		}else{
			bizId = goodsId;
		}

		MediaSource ms = null;
		Date date = new Date();
		String msg = "";
		try {
			String cameraIds = this.getPara("cameraIds");
			if(StrKit.notBlank(cameraIds)){
				String[] cameraIdArray = cameraIds.split(",");
				for(String cameraIdStr : cameraIdArray){
					int cameraId = Integer.valueOf(cameraIdStr);
					Camera c = Camera.dao.findById(cameraId);
					List<MediaSource> msList = MediaSource.dao.find("select * from media_source where media_type='LIVE' and biz_type=? and biz_id=? and content=? ", bizType, bizId, cameraId);
					boolean flag = true;
					for(MediaSource mediaSource : msList){
						if(mediaSource.getStr("content").equals(cameraIdStr)){
							flag = false;
							break;
						}
					}
					if(flag){
						ms = new MediaSource();

						ms.set(MediaSource.media_type, "LIVE");
						ms.set(MediaSource.cover, c.getStr("pic_url"));
						ms.set(MediaSource.content, cameraId);

						ms.set(MediaSource.biz_type, bizType);
						ms.set(MediaSource.biz_id, bizId);
						ms.set(MediaSource.status, 1);
						ms.set(MediaSource.sort, 0);
						ms.set(MediaSource.created_time, date);
						ms.set(MediaSource.updated_time, date);

						ms.save();
					}
				}
			}

			String videoIds = this.getPara("videoIds");
			if(StrKit.notBlank(videoIds)){
				String[] videoIdArray = videoIds.split(",");
				for(String videoIdStr : videoIdArray){
					int videoId = Integer.valueOf(videoIdStr);
					Video v = Video.dao.findById(videoId);
					List<MediaSource> msList = MediaSource.dao.find("select * from media_source where media_type='VIDEO' and biz_type=? and biz_id=? and content=? ", bizType, bizId, v.getStr("vid"));
					boolean flag = true;
					for(MediaSource mediaSource : msList){
						if(mediaSource.getStr("content").equals(v.getStr("vid"))){
							flag = false;
							break;
						}
					}
					if(flag){
						ms = new MediaSource();

						ms.set(MediaSource.media_type, "VIDEO");
						ms.set(MediaSource.cover, v.getStr("first_image"));
						ms.set(MediaSource.content, v.getStr("vid"));

						ms.set(MediaSource.biz_type, bizType);
						ms.set(MediaSource.biz_id, bizId);
						ms.set(MediaSource.status, 1);
						ms.set(MediaSource.sort, 0);
						ms.set(MediaSource.created_time, date);
						ms.set(MediaSource.updated_time, date);

						ms.save();
					}
				}
			}
			msg = "保存成功！";
			rendJson(true, msg, bizId, new Random().nextLong());

		} catch (Exception e) {
			log.error("保存异常", e);
			rendJson(false, "保存异常！");
			e.printStackTrace();
		}
	}
	
	//删除视频
	public void deleteMediaSource(){
		String id = this.getPara("id");
		MediaSource.dao.deleteById(id);
		String msg = "删除成功！";
		id = null;
		rendJson(true, msg, id, new Random().nextLong());
	}
	
	
	//修改视频封面或视频排序
	public void updateMediaSource(){

		Integer mediaId = getParaToInt("mediaId", -1);
		String coverUrl = getPara("coverUrl", "");
		Integer sort = getParaToInt("sort", 0);

		Date date = new Date();
		String msg = "";
		try {
			MediaSource ms = MediaSource.dao.findById(mediaId);

			ms.set(MediaSource.cover, coverUrl);
			ms.set(MediaSource.sort,sort);
			ms.set(MediaSource.updated_time, date);

			ms.update();
			msg = "更新成功！";
			rendJson(true, msg, mediaId, new Random().nextLong());

		} catch (Exception e) {
			log.error("更新异常", e);
			rendJson(false, "更新异常！");
			e.printStackTrace();
		}
	}

}
